package org.custom.spring.boot.security.service.impl;

import org.custom.spring.boot.datasource.entity.UserInfo;
import org.custom.spring.boot.datasource.service.UserInfoObtainService;
import org.custom.spring.boot.security.entity.SystemUser;
import org.custom.spring.boot.security.mapper.SystemRoleMapper;
import org.custom.spring.boot.security.mapper.SystemUserMapper;
import org.custom.spring.boot.security.service.SystemUserInfoService;
import org.custom.spring.boot.security.utils.SecurityUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 系统用户相关操作接口实现类
 *
 * @author LiKun
 * @date 2021/11/9 13:34
 */
@Validated
@Service("systemUserService")
public class SystemUserInfoServiceImpl implements SystemUserInfoService, UserInfoObtainService {

    @Resource
    private transient SystemRoleMapper systemRoleMapper;
    @Resource
    private transient SystemUserMapper systemUserMapper;

    @Override
    public SystemUser findSystemUserByUsername(String username) {
        return systemUserMapper.selectByUsername(username);
    }

    @Override
    public SystemUser findSystemUserByUserId(String userId) {
        SystemUser user = systemUserMapper.selectById(userId);
        return user.setAuthorities(systemRoleMapper.selectByUserId(userId));
    }

    @Override
    public UserInfo obtainUserInfo() {
        String userId = SecurityUtil.getCurrentUserId();
        SystemUser systemUser = systemUserMapper.selectById(userId);
        return UserInfo.builder().userId(userId).username(systemUser.getUsername()).build();
    }

}
